package com.burgeon.backend.Rest.User;
/**
 * 判断用户是否存在
 */

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.entity.User;
import com.burgeon.backend.service.ex.UserNotFoundException;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;
import java.util.ArrayList;
import java.util.HashMap;

public class UserExist extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public UserExist(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     */

    public User getData(String phone) throws Exception {
        String url = "http://"+ip+"/servlets/binserv/Rest";

        String str = RESULT(Data(phone),url);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)){
            throw new UserNotFoundException("出现错误");
        }
        String count = new JSONArray(str).getJSONObject(0).getString("count");
        if ("0".equals(count)){
            throw new UserNotFoundException("卡号未找到");
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        JSONArray array = jsonArray.getJSONArray(0);

        User user = new User();
        user.setName(array.getString(0));
        user.setPhone(array.getString(1));
        user.setIntegral(array.getString(2));
        user.setId(array.getString(3));
        user.setType(array.getString(4));
        user.setStore(array.getString(5));
        user.setCreateTime(array.getString(6));
        user.setEmployee(array.getString(7));
        user.setGender(array.getString(8));
        user.setBirthday(array.getString(9));
        return user;
    }
    private MultiValueMap Data(String phone) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("VIPNAME");
        columns.add("CARDNO");
        columns.add("INTEGRAL");
        columns.add("ID");
        columns.add("'C_VIPTYPE_ID;NAME'");//等级
        columns.add("'C_STORE_ID;NAME'");//店仓
        columns.add("OPENCARDDATE");//开卡时间
        columns.add("'HR_EMPLOYEE_ID;NAME'");//开卡人
        columns.add("SEX");//性别
        columns.add("BIRTHDAY");//生日

        params.put("columns", columns);
        params.put("table", "C_VIP");//表名

        HashMap paramss = new HashMap();
        paramss.put("column", "MOBIL");
        paramss.put("condition", phone);
        params.put("params", paramss);
        params.put("count","true");
        return DATA("Query", params,account,password);
    }


    public static void main(String[] args) {
        UserExist g = new UserExist("39.104.52.2:55321","zhaohong@bestone.net","666888");
        try {
            g.getData("17732141036");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
